{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import re\n",
    "\n",
    "def language_detection_textcleaning(string):\n",
    "    string = re.sub(\n",
    "        'http\\\\S+|www.\\\\S+',\n",
    "        '',\n",
    "        ' '.join(\n",
    "            [i for i in string.split() if i.find('#') < 0 and i.find('@') < 0]\n",
    "        ),\n",
    "    )\n",
    "\n",
    "    chars = ',.()!:\\'\"/;=-'\n",
    "    for c in chars:\n",
    "        string = string.replace(c, f' {c} ')\n",
    "    string = string.replace('\\n', '').replace('\\t', '')\n",
    "\n",
    "    string = re.sub(\n",
    "        '[0-9!@#$%^&*()_\\\\-+{}|\\\\~`\\'\";:?/.>,<]', ' ', string, flags=re.UNICODE\n",
    "    )\n",
    "    string = re.sub(r'[ ]+', ' ', string).strip()\n",
    "\n",
    "    return string.lower()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "\n",
    "files = [\n",
    "    'prepare-english-en.jsonl',\n",
    "    'prepare-english-ms.jsonl',\n",
    "    'prepare-manglish-en.jsonl',\n",
    "    'prepare-manglish-manglish.jsonl',\n",
    "    'filter-twitter-id.jsonl',\n",
    "    'prepare-indon.jsonl',\n",
    "    'prepare-indon-standard.jsonl',\n",
    "    'filter-malay-rojak-rojak.jsonl',\n",
    "    'filter-malay-rojak-ms.jsonl',\n",
    "    'filter-malay-rojak-en.jsonl',\n",
    "    'prepare-malay-ms.jsonl',\n",
    "    'standard-mandarin.jsonl',\n",
    "    'filter-twitter-malay-rojak-ms.jsonl',\n",
    "    'filter-twitter-malay-rojak-rojak.jsonl',\n",
    "    'filter-twitter-malay-rojak-ms.jsonl',\n",
    "    'filter-twitter-malay-rojak-id.jsonl',\n",
    "    'local-mandarin.jsonl',\n",
    "    'standard-mandarin.jsonl',\n",
    "    'prepare-standard-mandarin.jsonl',\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "4778606it [03:09, 25276.82it/s]\n",
      "100%|█████████████████████████████| 4534690/4534690 [00:25<00:00, 175293.42it/s]\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from tqdm import tqdm\n",
    "\n",
    "for f in files:\n",
    "    filename = f'{f}.dedup'\n",
    "    if os.path.exists(filename):\n",
    "        continue\n",
    "        \n",
    "    data = []\n",
    "    with open(f) as fopen:\n",
    "        for l in tqdm(fopen):\n",
    "            l = json.loads(l)\n",
    "            if not l:\n",
    "                continue\n",
    "            l = language_detection_textcleaning(l)\n",
    "            if len(l):\n",
    "                data.append(l)\n",
    "    \n",
    "    data = list(set(data))\n",
    "    with open(filename, 'w') as fopen:\n",
    "        for d in tqdm(data):\n",
    "            fopen.write(f'{json.dumps(d)}\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'null\\n'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'prepare-indon-standard.jsonl'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
